﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace StartupSolution.Views.Charts
{
    public partial class Theming : Page
    {
        public Theming()
        {
            InitializeComponent();
             
        }
        protected List<Theme> DataChartThemes;
        
        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            const string themeLocation = "/StartupSolution;component/Themes/";

            // set locations for xamDataChart themes
            Uri uriIgTheme = new Uri(themeLocation + "IgTheme/IG.xamDataChart.xaml", UriKind.Relative);
            Uri uriOfficeBlueTheme = new Uri(themeLocation + "Office2010Blue/Office2010Blue.xamDataChart.xaml", UriKind.Relative);

            this.DataChartThemes = new List<Theme>
            {
                new Theme {Name = "Default Theme", Uri = null}, 
                new Theme {Name = "IG Theme", Uri = uriIgTheme}, 
                new Theme {Name = "Office 2010 Blue Theme", Uri = uriOfficeBlueTheme},
            };

            this.themeSelectionComboBox.DisplayMemberPath = "Name";
            this.themeSelectionComboBox.ItemsSource = this.DataChartThemes;
            this.themeSelectionComboBox.SelectionChanged += OnSelectionChanged;
            this.themeSelectionComboBox.SelectedIndex = 1; // select IG Theme

            this.btnNext.Click += OnNextItemButtonClick;
            this.btnPrevious.Click += OnPreviousItemButtonClick;
        }
        private void OnPreviousItemButtonClick(object sender, RoutedEventArgs e)
        {
            if (this.themeSelectionComboBox.SelectedIndex == 0)
            {
                this.themeSelectionComboBox.SelectedIndex = themeSelectionComboBox.Items.Count - 1;
            }
            else
            {
                this.themeSelectionComboBox.SelectedIndex -= 1;
            }
        }
        private void OnNextItemButtonClick(object sender, RoutedEventArgs e)
        {
            this.themeSelectionComboBox.SelectedIndex = (this.themeSelectionComboBox.SelectedIndex + 1) % this.themeSelectionComboBox.Items.Count;
        }
        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //remove the current theme for xamDataChart control 
            PresentationFrameworkCollection<ResourceDictionary> resources = Application.Current.Resources.MergedDictionaries;
            for (int i = 0; i < resources.Count; i++)
            {
                if (resources[i].Source.OriginalString.Contains("xamDataChart"))
                {
                    resources.RemoveAt(i);
                }
            }
            // add selected theme for xamDataChart control
            Theme theme = (Theme)this.themeSelectionComboBox.SelectedItem;
            ResourceDictionary themeDictionary = new ResourceDictionary { Source = theme.Uri };

            resources.Add(themeDictionary);
   
        }
    }

    #region Theme class
    public class Theme
    {
        public bool IsDefault { get; set; }
        public string Id { get; set; }
        public string Name { get; set; }
        public Uri Uri { get; set; }
        public Theme()
        {
            IsDefault = false;
        }

        public Theme(string idx, string name, Uri uri)
        {
            Id = idx;
            Name = name;
            Uri = uri;
        }
    }
    #endregion Theme class
}

